package com.atguigu.edusta.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.edusta.client.StaClient;
import com.atguigu.edusta.entity.StatisticsDaily;
import com.atguigu.edusta.mapper.StatisticsDailyMapper;
import com.atguigu.edusta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-08
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private StaClient staClient;

    //远程调用得到某一天注册人数
    @Override
    public void registerCount(String day) {
        System.out.println("sta" + day);
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        R registerR = staClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");
        System.out.println(countRegister);
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //把获取数据添加到数据库，统计分析表里面
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister); //注册人数
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day); //统计日期

        baseMapper.insert(daily);
    }

    //图标显示，返回两部分数据，日期json格式，以及数量json格式
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculated = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            //封装日期list集合
            StatisticsDaily daily = staList.get(i);
            //list.add()加值 list.set()改值
            date_calculated.add(daily.getDateCalculated());

            //封装对应数量
            switch (type){
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        //把封装之后两个list集合放到map集合，进行返回
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculated",date_calculated);
        map.put("numDataList",numDataList);
        return map;
    }
}
